<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="手机号" prop="agentMobile">
        <el-input
          v-model="queryParams.agentMobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理人姓名" prop="agentName">
        <el-input
          v-model="queryParams.agentName"
          placeholder="请输入代理人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col> -->

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="whiteList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" width="55" prop="id" />

      <el-table-column label="代理人姓名" align="center" prop="agentName" />
      <el-table-column label="代理人等级" align="center" prop="level" />
      <el-table-column label="手机号" align="center" prop="agentMobile" />
      <el-table-column label="可提现金额" align="center" prop="usePrice" />
      <el-table-column label="已提现金额" align="center" prop="txPrice" />
      <el-table-column label="添加时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isStatus == 0" type="danger">待审核</el-tag>
          <el-tag v-if="scope.row.isStatus == 1" type="success">通过</el-tag>
          <el-tag v-if="scope.row.isStatus == 2" type="danger">不通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.isStatus == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cancel:agent:auditing']"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleOrder(scope.row)"
            v-hasPermi="['cancel:agent:levelLogs']"
            >佣金订单记录</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleRecord(scope.row)"
            v-hasPermi="['cancel:agent:levelLogs']"
            >等级变更记录</el-button
          >
          <el-button
            v-if="scope.row.isStatus == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleWithdraw(scope.row)"
            v-hasPermi="['cancel:agent:auditing']"
            >申请提现记录</el-button
          >
          <el-button
            v-if="scope.row.isStatus == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCode(scope.row)"
            v-hasPermi="['cancel:agent:auditing']"
            >小程序</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改后台导入用户白名单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="代理人姓名" prop="agentName">
          <el-input
            v-model="form.agentName"
            placeholder="请输入代理人姓名"
            readonly
          />
        </el-form-item>
        <el-form-item label="手机号" prop="agentMobile">
          <el-input
            v-model="form.agentMobile"
            placeholder="请输入手机号"
            readonly
          />
        </el-form-item>
        <el-form-item label="是否通过" prop="isStatus">
          <el-radio-group v-model="form.isStatus">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="原因" prop="remark" v-if="form.isStatus == 2">
          <el-input
            type="textarea"
            v-model="form.remark"
            placeholder="请输入不通过原因"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 等级变更记录 -->
    <el-dialog
      title="等级变更记录"
      :visible.sync="isRecord"
      width="1000px"
      append-to-body
    >
      <el-table :data="recorrdList" style="width: 100%">
        <el-table-column
          prop="before"
          align="center"
          label="变更前"
        ></el-table-column>
        <el-table-column
          prop="after"
          align="center"
          label="变更后"
        ></el-table-column>
        <el-table-column
          prop="createdAt"
          align="center"
          label="添加时间"
        ></el-table-column>
        <el-table-column
          prop="updatedAt"
          align="center"
          label="更新时间"
        ></el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="isRecord = false">确 定</el-button>
        <el-button @click="isRecord = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 佣金订单记录 -->
    <el-dialog
      title="佣金订单记录"
      :visible.sync="isOrder"
      width="1000px"
      append-to-body
    >
      <el-table :data="orderList" style="width: 100%">
        <el-table-column prop="sn" align="center" label="订单编号" />
        <el-table-column prop="moneyAmount" align="center" label="订单金额" />
        <el-table-column prop="createdTime" align="center" label="下单时间" />
        <el-table-column prop="jifenAmount" align="center" label="积分金额" />
        <el-table-column prop="before" align="center" label="变更前-佣金" />
        <el-table-column prop="change" align="center" label="变更佣金" />
        <el-table-column prop="after" align="center" label="变更后-佣金" />
        <el-table-column prop="notes" align="center" label="备注" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="isOrder = false">确 定</el-button>
        <el-button @click="isOrder = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 小程序码 -->
    <el-dialog
      :title="titleCode"
      :visible.sync="openCode"
      width="500px"
      append-to-body
    >
      <el-image
        style="display: flex; justify-content: center"
        :src="xcxImgUrl"
        fit="fill"
      ></el-image>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAgent,
  checkAgent,
  levelLogs,
  agentOrder,
} from "@/api/system/agent";
import { getToken } from "@/utils/auth";
export default {
  name: "White",
  data() {
    return {
      isRecord: false,
      recorrdList: [],
      isOrder: false,
      orderList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 后台导入用户白名单表格数据
      whiteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openCode: false,
      titleCode: "",
      xcxImgUrl: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mobile: null,
        userName: null,
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/cancel/userWhite/importDataVir",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
        ],
        userName: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询后台导入用户白名单列表 */
    getList() {
      this.loading = true;
      listAgent(this.queryParams).then((response) => {
        this.whiteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleRecord(row) {
      this.isRecord = true;
      const { id: agentId } = row;
      levelLogs({ agentId }).then((res) => {
        this.recorrdList = res.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        agentMobile: null,
        agentName: null,
        isStatus: 1,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //小程序
    handleCode(row) {
      console.log(row);
      this.openCode = true;
      this.xcxImgUrl = row.xcxImgUrl;
      this.titleCode = "代理人" + row.agentName + "的小程序码";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加代理人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
    },
    // 佣金订单
    handleOrder(row) {
      console.log(row);
      this.isOrder = true;
      agentOrder({ agentId: row.id }).then((res) => {
        this.orderList = res.data;
      });
    },
    // 申请提现记录的跳转
    handleWithdraw(row) {
      console.log(row);
      this.$router.push({
        path: "/agent/agentwithDrawLog",
        query: { param: row.id },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          checkAgent(this.form).then((response) => {
            this.$modal.msgSuccess("添加成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除后台导入用户白名单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delWhite(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "/cancel/userWhite/importTemplate",
        {},
        `white_template_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/white/export",
        {
          ...this.queryParams,
        },
        `white_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
